package com.xiaodou.mjf.application.user.utils;

import com.xiaodou.mjf.application.basic.service.SmsService;
import com.xiaodou.mjf.application.core.service.AccountService;
import com.xiaodou.mjf.application.core.service.BankCardCashioService;
import com.xiaodou.mjf.application.user.service.MemberSecurityService;
import com.xiaodou.mjf.application.user.service.MemberService;
import com.xiaodou.mjf.entity.basic.Sms;
import com.xiaodou.mjf.entity.core.Account;
import com.xiaodou.mjf.entity.core.BankCardCashio;
import com.xiaodou.mjf.entity.user.Member;
import com.xiaodou.mjf.entity.user.MemberSecurity;
import com.xiaodou.mjf.infrastructure.common.context.SpringContextHolder;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;

/**
 * Created by Rocky on 15/3/19.
 */
public class MemberUtils {

    private static SmsService smsService = SpringContextHolder.getBean(SmsService.class);
    private static AccountService accountService = SpringContextHolder.getBean(AccountService.class);
    private static MemberService memberService = SpringContextHolder.getBean(MemberService.class);
    private static MemberSecurityService memberSecurityService = SpringContextHolder.getBean(MemberSecurityService.class);
    private static BankCardCashioService bankCardCashioService = SpringContextHolder.getBean(BankCardCashioService.class);
    

    /**
     * 判断验证码是否正确
     *@param phone 手机号
     *@param code 验证码
     *@param type 下发短信验证码类型
     * */
    public static boolean verifyCode(String phone, String code, Integer type) {
        Sms sms = smsService.findByPhone(phone, type);
        if (sms != null && DateUtils.compare(DateUtils.getCurrentTime(), sms.getExpiryTime()) < 0) {
            if(sms.getCode().equals(code)){
                sms.setStatus(Sms.DELETE);
                smsService.save(sms);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取用户信息
     * @param identifier
     * @return
     */
    public static Member getMember(String identifier){

        MemberSecurity ms = memberSecurityService.getUserinfo(identifier);
        if(ms != null){
            Member member = memberService.get(ms.getId());
            return member;
        }
        return null;

    }

    /**
     * 获取用户账户
     * @param memberId  会员编号
     * @return  用户账户
     */
    public static Account getAccount(String memberId){
        Account account = accountService.getInfoBymemberId(memberId);
        return account;
    }

    public static Account getAccountByMobile(String mobile){
        MemberSecurity memberSecurity = memberSecurityService.getMember(mobile);
        Account account = accountService.getInfoBymemberId(memberSecurity.getId());
        return account;
    }

    public static void setRealName(String memberId){
        Member member = memberService.get(memberId);
        if(!member.getAuthType().equals(Member.AUTHED)){
            member.setAuthType(Member.AUTHED);
            memberService.save(member);
        }
    }
    public static void addCashIn(String memberId,String cardNumber,double amount){
    	BankCardCashio bankCardCashio=bankCardCashioService.findBycardnumber(memberId, cardNumber, 0);
        bankCardCashio.setCashIn(bankCardCashio.getCashIn()==null?amount:(bankCardCashio.getCashIn().doubleValue()+amount));
        bankCardCashioService.save(bankCardCashio);
    }
}
